package com.example.backend.service.impl;

import com.example.backend.entity.Vehicle;
import com.example.backend.repository.VehicleRepository;
import com.example.backend.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    private VehicleRepository vehicleRepository;
    //增
    @Override
    public Vehicle save(Vehicle vehicle) {
        return vehicleRepository.save(vehicle);
    }
    //删
    @Override
    public void delete(Integer id) {
        Vehicle vehicle = vehicleRepository.findById(id).get();
        vehicleRepository.delete(vehicle);
    }
    //改
    @Override
    public Vehicle update(Vehicle vehicle) {
        return vehicleRepository.save(vehicle);
    }
    //查
    //车辆编号
    @Override
    public List<Vehicle> findById(Integer id) {
        if(!vehicleRepository.findById(id).isPresent()) {
            return null;
        }
        else {
            Vehicle i = vehicleRepository.findById(id).get();
            return List.of(i);
        }
    }
    //车辆类型
    @Override
    public List<Vehicle> findByVehicleCategory(String vehicleCategory) {
        List<Vehicle> v = vehicleRepository.findByVehicleCategory(vehicleCategory);
        return v;
    }
    //车辆名称
    @Override
    public List<Vehicle> findByName(String name) {
        List<Vehicle> n = vehicleRepository.findByName(name);
        return n;
    }
    //购入时间
    @Override
    public List<Vehicle> findByBuyDate(String buyDate) {
        List<Vehicle> b = vehicleRepository.findByBuyDate(buyDate);
        return b;
    }
    //车辆状态
    @Override
    public List<Vehicle> findByStatus(String status) {
        List<Vehicle> s = vehicleRepository.findByStatus(status);
        return s;
    }
    //租金标准
    @Override
    public List<Vehicle> findByRent(Integer rent) {
        List<Vehicle> r = vehicleRepository.findByRent(rent);
        return r;
    }
    //车牌编号
    @Override
    public List<Vehicle> findByLicenseNumber(String licenseNumber) {
        List<Vehicle> l = vehicleRepository.findByLicenseNumber(licenseNumber);
        return l;
    }

    @Override
    public boolean findName(String name) {
        System.out.println(vehicleRepository.findByName(name));

        if(vehicleRepository.findByName(name).isEmpty()){
            return false;
        }
        return true;
    }

    @Override
    public boolean findNameAndId(Integer id, String name) {
        if(vehicleRepository.findByName(name).isEmpty()){
            return false;
        }
        else if(vehicleRepository.findByName(name).get(0).getId()==id){
            return false;
        }
        else{
            return true;
        }
    }
    //所有记录
    @Override
    public List<Vehicle> findAll() {
        return vehicleRepository.findAll();
    }

}
